package com.zs.zcerp.remark.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsResponse;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.remark.entity.RemarkUser;
import com.zs.zcerp.remark.entity.TgPpsM;
import com.zs.zcerp.remark.service.ITgPpsMService;
import com.zs.zcerp.system.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (TgPpsM)表控制层
 *
 * @author makejava
 * @since 2021-04-15 07:58:43
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("sys")
public class TgPpsMController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ITgPpsMService tgPpsMService;

    @GetMapping("list/{tabId}")
    public FebsResponse list(@PathVariable("tabId") Integer tabId, QueryRequest request){
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        RemarkUser remarkUser = new RemarkUser();
        remarkUser.setScoreUser(user.getUsername());
        remarkUser.setTabId(tabId);
        return new FebsResponse().success()
                .data(getDataTable(tgPpsMService.findRemarkLists(remarkUser, request)));
    }

    @GetMapping("proposal/{tabId}")
    public FebsResponse proposal(@PathVariable("tabId") Integer tabId, QueryRequest request){
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        RemarkUser remarkUser = new RemarkUser();
        remarkUser.setScoreUser(user.getUsername());
        remarkUser.setTabId(tabId);
        return new FebsResponse().success()
                .data(getDataTable(tgPpsMService.listProposal(remarkUser, request)));
    }

    @PostMapping("proposal/save")
    public FebsResponse proposalSave(@RequestBody TgPpsM tgPpsM){
        if (tgPpsM.getReasonDesc().isEmpty() || tgPpsM.getDefectDesc().isEmpty() || tgPpsM.getProposeDesc().isEmpty() || tgPpsM.getResulrDesc().isEmpty()) {

        }
        try {
            tgPpsMService.saveOrUpdate(tgPpsM);
        } catch (Exception e) {
            return new FebsResponse().fail().message("保存失败: " + e.getMessage());
        }
        return new FebsResponse().success().message("保存成功");
    }

    @GetMapping("proposal/get/{ppsNo}")
    public FebsResponse proposal(@PathVariable(value = "ppsNo", required = false) String ppsNo){
        if (ppsNo == null) {
            return new FebsResponse().success();
        }
        return new FebsResponse().success()
                .data(tgPpsMService.getByPpsNo(ppsNo));
    }

}